package com.zz.framework.core.utils


import com.zz.framework.R
import com.zz.framework.core.BaseApp
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*


object TimeUtil {
    val FORMAT_YYYYMM = "yyyy-MM"
    val FORMAT_YYYY = "yyyy"
    val FORMAT_MM = "MM"
    val FORMAT_DD = "DD"
    val FORMAT_YYYYMMDD = "yyyy-MM-dd"
    val FORMAT_YYYYMMDD_2 = "yyyy/MM/dd"

    val FORMAT_YYYYMM01 = "yyyy-MM-01"
    val FORMAT_YYYYMMDDHHMM = "yyyy-MM-dd HH:mm"
    val FORMAT_YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss"
    val FORMAT_HHMMSS = "yyyy-MM-dd HH:mm:ss +0000"
    val FORMAT_HHMM = "HH:mm"
    val FORMAT_MM_DD_HHMM = "MM/dd HH:mm"

    val currentDate: String
        get() {
            val calendar = Calendar.getInstance()
            return TimeUtil.format(calendar.timeInMillis, TimeUtil.FORMAT_YYYYMMDD)
        }

    val currentMonth: Int
        get() {
            val calendar = Calendar.getInstance()
            return calendar.get(Calendar.MONTH)
        }
    val currentDay: Int
        get() {
            val calendar = Calendar.getInstance()
            return calendar.get(Calendar.DAY_OF_MONTH)
        }
    val currentYear: Int
        get() {
            val calendar = Calendar.getInstance()
            return calendar.get(Calendar.YEAR)
        }

    fun formatValidityDate(dateBegin: Long, dateEnd: Long): String {
        return BaseApp.context.getString(
            R.string.text_money_date,
            format(dateBegin, FORMAT_YYYYMMDD),
            format(dateEnd, FORMAT_YYYYMMDD)
        )
    }

    fun format(date: Long, formatStr: String): String {
        if (date == 0L) {
            return "    暂无    "
        }
        val format = SimpleDateFormat(formatStr)
        return format.format(Date(date))
    }

    fun parse(str: String, formatStr: String): Long {
        val format = SimpleDateFormat(formatStr)
        try {
            return format.parse(str)!!.time
        } catch (e: ParseException) {
            return 0
        }

    }

    fun getSeason(date: Date?): Int {
        var season = 0
        val c = Calendar.getInstance()
        if (date != null) c.time = date
        val month = c.get(Calendar.MONTH)
        when (month) {
            Calendar.JANUARY, Calendar.FEBRUARY, Calendar.MARCH -> season = 1
            Calendar.APRIL, Calendar.MAY, Calendar.JUNE -> season = 2
            Calendar.JULY, Calendar.AUGUST, Calendar.SEPTEMBER -> season = 3
            Calendar.OCTOBER, Calendar.NOVEMBER, Calendar.DECEMBER -> season = 4
            else -> {
            }
        }
        return season
    }


    //毫秒转秒
    fun long2String(time: Long): String? {
        //毫秒转秒
        var sec = time.toInt() / 1000
        val min = sec / 60 //分钟
        sec = sec % 60 //秒
        return if (min < 10) {    //分钟补0
            if (sec < 10) {    //秒补0
                "0$min:0${sec}s"
            } else {
                "0$min:${sec}s"
            }
        } else {
            if (sec < 10) {    //秒补0
                "$min:0${sec}s"
            } else {
                "$min:${sec}s"
            }
        }
    }

    fun getDays(time1:Long,time2:Long):Int{
        var days = 0
        val c1 = Calendar.getInstance()
        val c2 = Calendar.getInstance()
        c1.timeInMillis  = time1
        c2.timeInMillis  = time2
        val year1 = c1.get(Calendar.YEAR)
        val year2 = c2.get(Calendar.YEAR)
        if (year1 == year2){ //同一年
            days = c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR)
        }else if (year1 < year2){
            //如果跨年了
            val day1 = c1.get(Calendar.DAY_OF_YEAR)
            val day2 = c2.get(Calendar.DAY_OF_YEAR)
            for (i in year1..year2){
                if(i == year1){
                    days += if(GregorianCalendar().isLeapYear(year1)){ //闰年
                        366 - day1
                    }else{
                        365 - day1
                    }
                }else if (i == year2){
                    days += day2
                }else{
                    days += if(GregorianCalendar().isLeapYear(year1)){
                        366
                    }else{
                        365
                    }
                }
            }
        }
        return days
    }
}
